

//货品选择器选择货品后的回调 isInit表示是是否初始化动作
function addProductSkuCallback(productSkuArray,isInit){
	if(productSkuArray.length != 0){
		$j("#addHint").hide();
	}
	for (var i = 0; i < productSkuArray.length; i++) {
		var productSku = productSkuArray[i];
		var jProductSkuExist = $j('#productSkuTable').find('input[name="productSkuCodes"][value="'+productSku.productSkuCode+'"]');
		if (jProductSkuExist.length != 0) {
			//已存在
			try {
				var jQuantity = jProductSkuExist.parent('td').find('input[name="skuQuantities"]');
				var quantity=new Number(jQuantity.val());
				jQuantity.val(quantity+1);
			}catch(err){
				
			}
		}else{	
			//不存在
			var html='<tr class="order_item">';
			var mediaUrl = productSku.imageUrl;
			var src_s0 = __mediaPath + mediaUrl.substring(0,mediaUrl.lastIndexOf("."))+"_s0"+mediaUrl.substring(mediaUrl.lastIndexOf("."));
			html += '<td><img src="'+src_s0+'" width="58" height="58" onerror="this.src=__defaultImage_s0;this.onerror=\'\'" /></img></td>';
			html += '<td>'+productSku.productSkuCode+'</td>';
			html += '<td>'+productSku.productName+'&nbsp;&nbsp;&nbsp;'+productSku.orderSkuDisplayOption+'</td>';
			html += '<td><input type="text" validconf="required,integer" name="skuQuantities" class="form-input"  style="width:80px;" id="skuQuantity'+productSku.productSkuId+'" value="' + productSku.minOrderQuantity + '"/>';
			html += '<input type="hidden" name="productSkuCodes" value="'+productSku.productSkuCode+'" />';
			html += '</td>';
			html += '<td align="center"><a href="javascript:void(0)" class="del" onclick="delOrderSku(this)">&nbsp;</a></td>';
			html += '</tr>';
			$j('#productSkuTable').append(html);
			applyValidate($('skuQuantity'+productSku.productSkuId),"required,integer");
		}	
	}
	closeDialogOfproductSkuSelector(); 
	
	if(isInit == null || !isInit){
		//重新加载收货地区和支付配送方式
		loadConsigneeAndPayShipMethodDiv4EditPanel();
	}
	
}


//加载送货地址和配送、支付方式
function loadConsigneeAndPayShipMethodDiv4EditPanel(){
	var jPanel = $j('#orderEditPanel');
	if(jPanel.find('#consigneeAndPayShipMethodDiv').length != 0){
		var params = jPanel.find(':input').serializeArray();
		jPanel.find('#orderDetail').loading().load(__ctxPath+'/order/salesOrder/blank.html?doAction=loadOrderInfo4EditPanel',params);
	}
}

//删除货品
function delOrderSku(orderSkuDOM){
	$j(orderSkuDOM).parents('tr:first').remove();
	if ($j('#productSkuTable').find('input[name="productSkuCodes"]').length == 0) {
		$j("#addHint").show();
	}
	
	//重新加载收货地区和支付配送方式
	loadConsigneeAndPayShipMethodDiv4EditPanel();
	
	
}

//从Form返回到list
function returnToList(){
	$j('#formDiv').hide();
	$j('#mainDiv').show();
}

//渲染地区修改区域 注意与salesOrderList.js有一个差不多名称的方法的区别
function renderEditPanelRegionModifyDiv(buttonDom){
	var jEditPanel = $j('#orderEditPanel');
	var regionId = jEditPanel.find('input[name="regionId"]').val();
	jEditPanel.find('#regionModifyDiv').loading().load(__ctxPath+'/order/salesOrder/blank.html?doAction=renderEditPanelRegionModify&regionId='+regionId);
	jEditPanel.find('#regionShowDiv').empty();
}
//渲染地区显示区域
function renderEditPanelRegionShowDiv(buttonDom){
	var jEditPanel = $j('#orderEditPanel');
	if(validateForm(jEditPanel.find('#regionModifyDiv').get(0))){
		var regionId = jEditPanel.find('input[name="regionId"]').val();
		jEditPanel.find('#regionShowDiv').loading().load(__ctxPath+'/order/salesOrder/blank.html?doAction=renderEditPanelRegionShow&regionId='+regionId);
		jEditPanel.find('#regionModifyDiv').empty();
		
		//当前订单id
		var salesOrderId = jEditPanel.find('#salesOrderId').val();
		
		//加载支付与配送方式
		var salesOrderId = jEditPanel.find('#salesOrderId').val();
		var params = jEditPanel.find(':input').serializeArray();
		jEditPanel.find('#payShipMethodDiv').loading().load(__ctxPath+'/order/salesOrder/blank.html?doAction=loadPayShipMethodDiv4EditPanel&regionId='+regionId+'&salesOrderId='+salesOrderId,params);
	}else{
		alert(__vaMsg.notPass);
	}
}

//更改支付方式 regionId必须从外部传入,因为送货地区与支付方式是同时load的
function changePaymentMethod(paymentMethodDOM,regionId){
	if(regionId == null || regionId == undefined){
		//地区为空则不需要改变配送方式和自提方式
		return;
	}
	
	var jEditPanel = $j('#orderEditPanel');
	var jSelectedPaymentMethodId = jEditPanel.find("input:radio[name='paymentMethodId']:checked");
	//是否货到付款
	var isCod = jSelectedPaymentMethodId.attr("isCod");
	//当前支付方式id
	var paymentMethodId = jSelectedPaymentMethodId.val();
	//当前订单id
	var salesOrderId = jEditPanel.find('#salesOrderId').val();
	
	//----是否显示付款方式---	
	if (isCod == 1) {
		$j("[group*='cod']").show();
	} else {
		$j("[group*='cod']").hide();
	}
	
	if (paymentMethodId == 5) {
		//case:自提方式
		$j("[group*='shippingMethod']").hide();
		$j("[group*='selfCollect']").show();
		
		
		$j("#shippingMethodList").empty();
		//提货点
		$j("#selfCollectionCentreList").loading().load(__ctxPath+'/order/salesOrder/blank.html?doAction=loadSelfCollectionCentre4EditPanel&regionId='+regionId+'&paymentMethodId='+paymentMethodId+'&salesOrderId='+salesOrderId);
	} else {
		//case:非自提方式
		$j("[group*='shippingMethod']").show();
		$j("[group*='selfCollect']").hide();
		
		$j("#selfCollectionCentreList").empty();
		//配送方式
		$j("#shippingMethodList").loading().load(__ctxPath+'/order/salesOrder/blank.html?doAction=loadShippingMethod4EditPanel&regionId='+regionId+'&paymentMethodId='+paymentMethodId+'&salesOrderId='+salesOrderId);
		
	}
	
}

//更改配送方式
function changeShippingMethod(shippingMethodDOM){
	
	var jSelectedShippingRateId = $j("#orderEditPanel input:radio[name='shippingRateId']:checked");
	//是否支持指定配送时间
	var supportSpecifyingDeliveryTime = jSelectedShippingRateId.attr("supportSpecifyingDeliveryTime");
	if (supportSpecifyingDeliveryTime  == 1) {
		$j("[group*='specifiedDeliveryTime']").show();
	} else {
		$j("[group*='specifiedDeliveryTime']").hide();
	}
}

//改变是否支持发票
function toggleHasInvoice(checkboxDOM){
	var jEditPanel = $j('#orderEditPanel');
	if (checkboxDOM.checked) {
		jEditPanel.find("#hasInvoice").val("1");
		jEditPanel.find("#invoiceTitle").val("");
		applyValidate(jEditPanel.find("#invoiceTitle").get(0),"required,maxlength=128");
		jEditPanel.find("#invoiceTitleDiv").show();
	} else {
		jEditPanel.find("#hasInvoice").val("0");
		jEditPanel.find("#invoiceTitle").val("");
		removeValidate(jEditPanel.find("#invoiceTitle").get(0));
		jEditPanel.find("#invoiceTitleDiv").hide();
	}
}

//改变包装
function changeWrap(selectDOM){
	var jEditPanel = $j('#orderEditPanel');
	if(selectDOM.value != ""){
		jEditPanel.find("#wrapNote").val("");
		jEditPanel.find("#wrapNoteDiv").show();
	}else{
		jEditPanel.find("#wrapNote").val("");
		jEditPanel.find("#wrapNoteDiv").hide();
	}
}

//显示预览面板
function showPreviewPanel4Edit(){
	$j('#editPreviewPanel').parents('.ui-dialog:first').find('.ui-dialog-buttonpane').show(); //预览窗口按钮显示
	if(checkOrder()){
		var params = $j('#orderEditPanel :input').serializeArray();
		dlgeditPreviewPanel_show(params);
	}
}

//保存订单
function saveOrder(){
	if(checkOrder()){
		if(validateForm($('editPreviewPanel'))){
			$j('#editPreviewPanel').parents('.ui-dialog:first').find('.ui-dialog-buttonpane').hide(); //预览窗口按钮隐藏
			$j('#editPreviewPanel #proccessing').loading4();
			var params =$j('#orderEditPanel :input, #editPreviewPanel :input').serializeArray();
			$j.post(__ctxPath+'/order/salesOrder/blank.html?doAction=saveOrder',params,saveOrderCallback,"json");
		}else{
			alert(__vaMsg.notPass);
			return false;
		}
	}
}

//保存订单callback
function saveOrderCallback(data){
	if(data.error){
		alert(data.error);
		dlgeditPreviewPanel_close();
		return;
	}
	dlgeditPreviewPanel_close();
	returnToList();
	refreshOrderItem(data.salesOrderId);  //只需刷新局部，如果是搜索结果，则不一定最优
	sysMsg(__FMT.salesOrder_editPreviewPanel_saveSuccess+data.orderNo+__FMT.salesOrder_editPreviewPanel_saveSuccess2);		
}

//刷新父窗口的产品列表
function refreshParent(){
	refreshOrderList();
}

//检查下单资料
function checkOrder(){
	var jPanel = $j('#orderEditPanel');
	var isConsigneeVirtual = false;
	if(jPanel.find('#isConsigneeVirtual').length != 0){
		isConsigneeVirtual = true;
	}
	
	if(jPanel.find('input[name="productSkuCodes"]').length == 0 && $j('#orderEditPanel').find(':input[name="giftCertAmts"]').length == 0){
		//检查是否完全没有物品
		alert(__FMT.salesOrder_editPanel_tip_noItem_disabled);
		return false;
	} 
	if(!isConsigneeVirtual && jPanel.find('#regionShowDiv input[name="regionFullName"]').length != 0 && jPanel.find('#regionShowDiv input[name="regionFullName"]').val().trim() ==""){
		//检查收货地区是否已填写
		alert(__FMT.salesOrder_panel_region_needModify);
		return false;
	}
	if(!isConsigneeVirtual && jPanel.find('#regionShowDiv input[name="regionId"]').length == 0){
		//检查收货地区是否已确定
		alert(__FMT.salesOrder_panel_region_needConfirm);
		return false;
	}
	
	if(!validateForm(jPanel.get(0))){
		alert(__vaMsg.notPass);
		return false;
	}
	return true;
}
